import moment from 'moment'
import { transformTimeZone } from '@/filters'
function handle (data, type = 'day', reverse, dateFormat, conditionFormat, order) {
  if (!dateFormat) {
    dateFormat = 'MMM DD, YYYY'
  }
  if (reverse) {
    data.reverse()
  }
  const dateArray = getDateArray(data, type)
  const sliceIndex = sliceArrayIndex(dateArray, type)
  // result
  return resultData(sliceIndex, data, type, order, dateFormat, conditionFormat)
}

function getDateArray (data, type) {
  return data.map((item, index) => {
    switch (type) {
      case 'day':
        return index
      case 'week':
        return moment(item.date).weekday(0).format('YYYYMMDD')
      case 'month':
        return `${moment(item.date).year()}${moment(item.date).month()}`
      case 'quarter':
        return `${moment(item.date).year()}${moment(item.date).quarter()}`
      case 'year':
        return moment(item.date).year()
      default:
        break
    }
  })
}

function resultData (sliceIndex, data, type, order, dateFormat, conditionFormat) {
  const obj = {
    columns: [],
    data: [],
    columnsKey: {}
  }
  sliceIndex.map((item, index) => {
    const weekData = sliceArray(sliceIndex[index - 1], item, data)
    if (weekData.length > 0) {
      let count = 0
      let field = null
      let headerText = null
      const timezone = weekData[0].date
      const sortDate =  transformTimeZone(weekData[0].date,'YYYY/MM/DD')
      field = `${transformTimeZone(weekData[0].date, 'YYYY/MM/DD')}${transformTimeZone(weekData[weekData.length - 1].date, 'YYYY/MM/DD')}`
      headerText = `${transformTimeZone(weekData[0].date, dateFormat )} - ${transformTimeZone(weekData[weekData.length - 1].date, dateFormat)}`
      if (moment(weekData[0].date).isSame(moment(weekData[weekData.length - 1].date))) {
        headerText = `${transformTimeZone(weekData[0].date,dateFormat)}`
      }
      if (order === 'n') {
        field = `${transformTimeZone(weekData[weekData.length - 1].date, 'YYYY/MM/DD')}${transformTimeZone(weekData[0].date, 'YYYY/MM/DD')}`
        headerText = `${transformTimeZone(weekData[weekData.length - 1].date, dateFormat)} - ${transformTimeZone(weekData[0].date, dateFormat)}`
        if (moment(weekData[0].date).isSame(moment(weekData[weekData.length - 1].date))) {
          headerText = `${transformTimeZone(weekData[0].date,dateFormat)}`
        }
      }
      if (type === 'day') {
        field = `${transformTimeZone(weekData[0].date, 'YYYY/MM/DD')}`
        headerText = `${transformTimeZone(weekData[0].date, dateFormat)}`
      }
      weekData.map((item, index) => {
        let numberReg = parseFloat(item.names)
        let countReg = parseFloat(count)

        if (index === weekData.length - 1) {
          count = item.formatValue(Number(countReg) + Number(numberReg))
        } else {
          count = Number(countReg) + Number(numberReg)
        }
      })
      obj.data.push({
        [field]: {
          count,
          style: compare(conditionFormat, count)
        }
      })
      obj.columns.push({
        headerText: headerText,
        field: field,
        sort: sortDate,
        timezone
      })
      obj.columnsKey[field] = true
    }
  })
  return obj
}

function sliceArray (start = 0, end, data) {
  return data.slice(start, end)
}

function sliceArrayIndex (dateArray, type) {
  let arr = []
  switch (type) {
    case 'day':
      arr = dateArray
      break
    case 'week':
    case 'month':
    case 'quarter':
    case 'year':
      let obj = {}
      dateArray.map((item, index) => {
        if (!obj[item]) {
          obj[item] = item
          arr.push(index)
        }
      })
      break
  }
  arr.push(dateArray.length)
  return arr
}


function compare (conditionFormat, count) {
  let obj = {
    backgroundColor: '',
    color: ''
  }
  if (conditionFormat.length > 0) {
    let arr = JSON.parse(JSON.stringify(conditionFormat)).reverse()
    arr.forEach(data => {
      if (data) {
        if (count === null) obj = {} // [it.dataForm.filters.rules[0].key]
        // eslint-disable-next-line
        var val = count.toString().replace(/[\$\,]/g, '')// [it.dataForm.filters.rules[0].key]
        if (val.includes('%')) {
          // eslint-disable-next-line
          val = Number(val.replace(/%/g, '')) / 100
        } else if (val.indexOf('k') > -1) {
          // eslint-disable-next-line
          val = Number(val.replace(/k/g, '')) * 1000
        }
        if (data.dataForm.filters.rules.length > 0 && (data.dataForm.filters.rules[0].opt === '==' || data.dataForm.filters.rules[0].opt === '!=')) {
          // eslint-disable-next-line
          if (data && eval((val).toString() + data.dataForm.filters.rules[0].opt + (data.dataForm.filters.rules[0].value).toString())) {
            obj = {
              backgroundColor: data.backgroundColor,
              color: data.textColor
            }
          }
        } else {
          // eslint-disable-next-line
          if (data.dataForm.filters.rules.length > 0 && data && eval(Number(val) + data.dataForm.filters.rules[0].opt + Number(data.dataForm.filters.rules[0].value))) {
            obj = {
              backgroundColor: data.backgroundColor,
              color: data.textColor
            }
          }
        }
      }
    })
    // conditionFormat
  }
  return obj
}

export default handle
